<template>
  <div class="ma-content-block">
    <sa-table ref="crudRef" :options="options" :columns="columns" :searchForm="searchForm">
      <!-- 搜索区 tableSearch -->
      <template #tableSearch>
        <!-- <a-col :span="8">
          <a-form-item label="层级" field="level">
            <a-input v-model="searchForm.level" placeholder="请输入层级" />
          </a-form-item>
        </a-col> -->
        <a-col :span="8">
          <a-form-item label="名称" field="name">
            <a-input v-model="searchForm.name" placeholder="请输入名称" />
          </a-form-item>
        </a-col>
      </template>
	  <template #operationBeforeExtend="{ record }">
	    <a-link @click="openAdd(record)" v-if="record.level < 3"  v-auth="['/Cnarea/Cnarea/save']"> <icon-plus /> 新增 </a-link>
	  </template>

      <!-- Table 自定义渲染 -->
    </sa-table>

    <!-- 编辑表单 -->
    <edit-form ref="editRef" @success="refresh" />
  </div>
</template>

<script setup>
import { onMounted, ref, reactive } from 'vue'
import { Message } from '@arco-design/web-vue'
import EditForm from './edit.vue'
import api from '../api/cnarea'

// 引用定义
const crudRef = ref()
const editRef = ref()

// 搜索表单
const searchForm = ref({
  level: '',
  name: '',
})


// SaTable 基础配置
const options = reactive({
  api: api.getPageList,
  rowSelection: { showCheckedAll: true },
  isExpand: false,
  add: {
    show: true,
    auth: ['/Cnarea/Cnarea/save'],
    func: async () => {
      editRef.value?.open()
    }
  },
  edit: {
    show: true,
    auth: ['/Cnarea/Cnarea/update'],
    func: async (record) => {
      editRef.value?.open('edit')
      editRef.value?.setFormData(record)
    },
  },
  delete: {
    show: true,
    auth: ['/Cnarea/Cnarea/destroy'],
    func: async (params) => {
      const resp = await api.delete(params)
      if (resp.code === 200) {
        Message.success(`删除成功！`)
        crudRef.value?.refresh()
      }
    },
  },
})

// SaTable 列配置
const columns = reactive([
  // { title: '层级', dataIndex: 'level', width: 180 },
  // { title: '邮政编码', dataIndex: 'zip_code', width: 180 },
  // { title: '区号', dataIndex: 'city_code', width: 180 },
  { title: '名称', dataIndex: 'name', width: 180 },
  { title: '简称', dataIndex: 'short_name', width: 180 },
  { title: '组合名', dataIndex: 'merger_name', width: 180 },
  { title: '拼音', dataIndex: 'pinyin', width: 180 },
  { title: '经度', dataIndex: 'lng', width: 180 },
  { title: '纬度', dataIndex: 'lat', width: 180 },
  
])

// 页面数据初始化
const initPage = async () => {}

// SaTable 数据请求
const refresh = async () => {
  crudRef.value?.refresh()
}
const openAdd = (record) => {
  editRef.value?.open()
  editRef.value?.setFormData({ parent_code: record.id,level:record.level+1 })
}
// 页面加载完成执行
onMounted(async () => {
  initPage()
  refresh()
})
</script>
